package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysFinancing;
import com.ruoyi.system.service.ISysFinancingService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.servlet.ModelAndView;

/**
 * 融资申请Controller
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/financing")
public class SysFinancingController extends BaseController
{
    @Autowired
    private ISysFinancingService sysFinancingService;

    /**
     * 111
     */
    /**
     * 查询融资申请列表
     */
    @RequiresPermissions("system:financing:list")
    @GetMapping("/list")
    public TableDataInfo list(SysFinancing sysFinancing)
    {
        startPage();
        List<SysFinancing> list = sysFinancingService.selectSysFinancingList(sysFinancing);
        return getDataTable(list);
    }

    /**
     * 111
     */
    /**
     * 导出融资申请列表
     */
    @RequiresPermissions("system:financing:export")
    @Log(title = "融资申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFinancing sysFinancing)
    {
        List<SysFinancing> list = sysFinancingService.selectSysFinancingList(sysFinancing);
        ExcelUtil<SysFinancing> util = new ExcelUtil<SysFinancing>(SysFinancing.class);
        util.exportExcel(response, list, "融资申请数据");
    }

    /**
     * 获取融资申请详细信息
     * 1561561
     */
    @RequiresPermissions("system:financing:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysFinancingService.selectSysFinancingById(id));
    }

    /**
     * 新增融资申请
     * 2151156156023
     */
    @RequiresPermissions("system:financing:add")
    @Log(title = "融资申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFinancing sysFinancing)
    {
        return toAjax(sysFinancingService.insertSysFinancing(sysFinancing));
    }

    /**
     * 修改融资申请
     */
    @RequiresPermissions("system:financing:edit")
    @Log(title = "融资申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFinancing sysFinancing)
    {
        ///116023
        return toAjax(sysFinancingService.updateSysFinancing(sysFinancing));
    }

    /**
     * 删除融资申请
     * 删除
     * ...
     *
     * ....
     */
    @RequiresPermissions("system:financing:remove")
    @Log(title = "融资申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysFinancingService.deleteSysFinancingByIds(ids));
    }



    /**
     * 融资审核
     */
    @RequiresPermissions("system:financing:edit")
    @Log(title = "融资申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult shenpi(@Valid JrRentInstallmentSearchPage jrRentInstallmentSearchPage, HttpServletRequest request)
    {

        //融资审批
        ModelAndView mv = ModelAndViewUtil.of("jinrong/togetherCheck_sub");

        //123456
        JrRentInstallmentSearchPage jrRentInstallmentSearch = new JrRentInstallmentSearchPage();
        jrRentInstallmentSearch.setPageNum(getPageNum(request));
        jrRentInstallmentSearch.setPageSize(10);

        if(jrRentInstallmentSearchPage.getJrProductType()!=null &&jrRentInstallmentSearchPage.getJrProductType()==-1){
            //252451
            jrRentInstallmentSearch.setJrProductType(null);
        }else{
            //2515151
            jrRentInstallmentSearch.setJrProductType(jrRentInstallmentSearchPage.getJrProductType());
        }
        if(jrRentInstallmentSearchPage.getApplyState()!=null && jrRentInstallmentSearchPage.getApplyState()==-1){
            //完成......
            jrRentInstallmentSearch.setApplyState(null);
        }else{
            jrRentInstallmentSearch.setApplyState(jrRentInstallmentSearchPage.getApplyState());
        }

        jrRentInstallmentSearch.setStartDate(jrRentInstallmentSearchPage.getStartDate());
        jrRentInstallmentSearch.setEndDate(jrRentInstallmentSearchPage.getEndDate());

        //lallalala
        financeService.searchLoans(jrRentInstallmentSearch);

        //完成审核加入
        mv.addObject("rentInstall", jrRentInstallmentSearch);

        //加入pageinfo    分页
        mv.addObject("pageInfo", jrRentInstallmentSearch.getPageInfo());

        //返回结果
        return toAjax(sysFinancingService.updateSysFinancing(sysFinancing));
    }






}
